<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>现场兑奖</title>
	<script language="javascript" src="../script/common.js"></script>
	<script src="../script/card.js"></script>
	<script language="javascript" type="text/javascript">
	    var i;
		//全局变量
		var org = Common.cOrg();	//当前单位
		var qt = {};
		var qtLocale = {};				//数据表格对象
		var qt1 = {};
		var qt3 = {};
		var cardUserCode;
		var account;
		var card; 
		var bookOrderid;
		var cardUsername;
		var tradeid;
		var balance;
		var frozenPoints;
		var giftruleid;
		var data ={};
		var acclevel;
		function load(){
		      Eui.showNavigation();
		      setPages(["listview","printview","selectdetview","showlistdiv","listviewmain"]);
		     $('listformmain').giftLocation.value=org.id;
		     $("white_enterform").giftName.value="";
		     $("white_enterform").numeral.value="";
        
		         createTable1();
			    showData1();	
		    
			
		}
		    function unload(){
	               DWR.call('giftStorageService.deleteGiftOrderByState',org,function(v){
		      
		          });
	               }
		function black(){
		if(bookOrderid!=null){
		 DWR.call('giftStorageService.deleteGiftOrderByState',function(v){
		        $("userid").innerHTML="";
	            $("username").innerHTML="";
	            $("points").innerHTML="";
	             $("vpoints").innerHTML="";
		       $('white_enterform').localeEncash.disabled=true;
		       $('white_enterform').numeral.disabled=true;
		       $("white_enterform").giftName.value="";
		     $("white_enterform").numeral.value="";
		       createTable1();
		       showData1();			
		       switchPage("listviewmain");    
		      });   
		}else{
		        $("white_enterform").giftName.value="";
		        $("white_enterform").numeral.value="";
		        $("vpoints").innerHTML="";
		        $('white_enterform').localeEncash.disabled=true;
		        $('white_enterform').numeral.disabled=true;
		         createTable1();	
		           showData1();			
		         switchPage("listviewmain");   
		}
		 
	           
		}
		function dayin(){
		         DWR.call('giftStorageService.freezeBookPoints',cardUserCode,bookOrderid,function(v){
		        	giftId =v.id;
			  	requestframe.document.location.href = "print_loy_realtime_prize.html";
			  	switchPage("printview"); 
		 });
		}
		 function  back(){ 
                 {
		        bookOrderid=null;
		        $("userid").innerHTML="";
	            $("username").innerHTML="";
	            $("points").innerHTML="";
	            $("vpoints").innerHTML="";
		        $('white_enterform').localeEncash.disabled=true;
		        $('white_enterform').numeral.disabled=true;
		        $("white_enterform").giftName.value="";
		        $("white_enterform").numeral.value="";
		        createTable1();
		        showData1();		
		        switchPage("listviewmain");  
		        }
		 }
		 
		function blacklist(){
		   bookOrderid=null;
		       createTable1();
		       showData1();		
		       switchPage("listviewmain");  
		}

			
	function deleteItem(v,r){
		 return "<a href=# onclick=\"deleteItems("+r[5]+");return false;\">删除</a>"
		}
		function deleteItems(bookItemid){
		      DWR.call('giftStorageService.deleteBookItem',bookItemid,function(v){
		           DWR.call('giftStorageService.getBookOrderPoints',bookOrderid,function(v){
	                 $("vpoints").innerHTML=Format.fenToYuan(v);
	                 $("kpoints").innerHTML=balance-Format.fenToYuan(v);
	                 frozenPoints=Format.fenToYuan(v);
	                });
	                
	                 $("points").innerHTML=balance; 
	                 createTableLocale();
	                 showDataLocale();
	                 $("selectdetview").style.display = "";     
		      
		      });   
		}
		function showlist(v,r){
		 return "<a href=# onclick=\"showlists('"+r[0]+"','"+r[1]+"','"+r[2]+"','"+r[3]+"',"+r[4]+",'"+r[5]+"','"+r[6]+"','"+r[7]+"');return false;\">查看</a>"
		}
		function showlists(code,state,name,plantime,vplantime,points,oper,id){
		 // var date1=new Date(plantime);
		//  var date2=new Date(vplantime); 
		
		          $("vorderid").innerHTML=code;
		          if(name==null){
		           $("usernames").innerHTML=""; 
		          } 
		          
		           $("orderstate").innerHTML=state; 
		           $("usernames").innerHTML=name; 
	               $("plantime").innerHTML=plantime;
	               $("point").innerHTML=points;
	               $("oper").innerHTML=oper;
	         
               $("listform1").bookorderCode.value=code;
               
			     createTable3();
			     showData3();
	               switchPage("showlistdiv");
	            
  	
		}
		function createTable3(){
		var columns =[];
	
		qt3= new QueryTable("createTable3_query","listform1","showlist",columns,"");	
		}
		function createTable1(){
		var columns =[];
			columns[6]={title:"查看",width:"15%",render:showlist}
		

			qt1 = new QueryTable("createTable1_query","listformmain","datalist1",columns,"");
		}
		function createTableLocale(){
		var columns =[];
		columns[5]={title:"操作",width:"15%",render:deleteItem}
	
	       qtLocale= new QueryTable("createQueryLocale_query","white_enterform","datalistLocale",columns,"");	
		}
		
		function setParentId(id){
		orgParentId=id;
		}
		 //新增时 读卡
		 function readCard(){
		   	try{
			      getCard();
				cardUserCode= getAsn();
 			}catch(e){
				showError(e.description);
				return;
			}
			if(cardUserCode=="") {
				showError("读卡失败，请重新读卡");
				return;
			} 
			$('white_enterform').localeEncash.disabled=false;
		     $('white_enterform').numeral.disabled=false;
		    DWR.call('giftStorageService.readCardGetCardUser',cardUserCode,function(v){
		             acclevel=v.currentLevel;      
		             });  
		  DWR.call('giftStorageService.readCardGetAccount',cardUserCode,function(v){
		    $("userid").innerHTML=cardUserCode;
		    $("points").innerHTML=Format.fenToYuan(v.balance);
		       $("kpoints").innerHTML=Format.fenToYuan(v.balance);
		       balance=Format.fenToYuan(v.balance);
		        account=v;
		        DWR.call('giftStorageService.readCardGetCustomer',account.id,function(v){
		          $("username").innerHTML=v.name;
	                 cardUsername=v.name;
		        });
	           
		         });
		}
		function djfunc(){
		
		 var num=$("numeral").value;
		 var num1=Number(num);
		if(num1<=0)
		    {
		      showQuestion("数量不可以小于0");
		    }
		   else
		   {
		    $("selectdetview").style.display ="";   
		     if(bookOrderid==null){
		          if(giftruleid!=null&num!=null){
		            DWR.call('giftStorageService.createBookOrder',account.id,cardUserCode,1,org.id,function(v){
                    $('white_enterform').readorderid.value=v.id;
	                $("vpoints").innerHTML=Format.fenToYuan(v.frozenPoint);
	                bookOrderid=v.id; 
	                   DWR.call('giftStorageService.addBookItem',giftruleid,bookOrderid,num,account.id,cardUserCode,function(v){
	                   if(v==1){
	                           showError("库存数量不足!");
	                          }
	                    else if(v==2){
	                   showError("积分不够!");
	                  }else{
	                      $("points").innerHTML=balance;
		                  DWR.call('giftStorageService.getBookOrderPoints',bookOrderid,function(v){ 
		                  $("kpoints").innerHTML=balance-Format.fenToYuan(v);
	                      $("vpoints").innerHTML=Format.fenToYuan(v); 
	                       });
	                     createTableLocale();
	                    showDataLocale();
	                    } 
	              });
		         });
		    }
		}else{
		       var b=false;
	           DWR.call('giftStorageService.isGiftRule',giftruleid,bookOrderid,function(v){
	           if(!v){
	           DWR.call('giftStorageService.addBookItem',giftruleid,bookOrderid,num,account.id,cardUserCode,function(v){
	               if(v==1){
	               showError("库存数量不足!");
	              }else if(v==2){
	                showError("积分不够!");
	              }else{
	                         $("points").innerHTML=balance;
	                         DWR.call('giftStorageService.getBookOrderPoints',bookOrderid,function(v){ 
	                         $("kpoints").innerHTML=balance-Format.fenToYuan(v);
	                         $("vpoints").innerHTML=Format.fenToYuan(v); 
	                         });
	                         createTableLocale();
	                         showDataLocale();
	                     }
	              });
	          }else{
	                  showError("你已经有些礼品了！");
	                  $("white_enterform").giftName.value="";
		             $("white_enterform").numeral.value="";
	               }
	            });
	         } 
		   }
		}
		function resetFunc(){
		     $('white_enterform').localeEncashs.disabled=false;
		     white_enterform.reset();
		     $("white_enterform").giftName.value="";
		     $("white_enterform").numeral.value="";
		     switchPage('listview');
		}
		function onNewBookOrder(){
		     account=null;
               card=null; 
               bookOrderid=null;
               cardUsername=null;
		     tradeid=null;
		     frozenPoints=null;
			switchPage("listview")
		}
		function onSave(){
			switchPage('selectdetview');
			var datatime = new Date();
			white_enterform.awardname.value = datatime.format();
			var white = Common.getValues(white_enterform);
			Common.showDetail("selectdetview",white);
			return;      
		}  
		function showData1(){

			qt1.showTable();
			//qt1为全局的QueryTable对象
		}
		function showData3(){	
			qt3.showTable();				//qt1为全局的QueryTable对象
		}
		function showDataLocale(){	
			qtLocale.showTable();				//qtLocale为全局的QueryTable对象
		}
		function selOrg(){			
			var v = openDialogFrame("/client/giftconvert_list.html?acclevel="+acclevel+"",'选择商品',500,700)
			if (v){		
                    $("giftName").value = v.name;
			      giftruleid=v.id;
			}
		}
	</script>
</HEAD>
<body onload="load()" onunload="unload()">
	<!--页面导航-->
	<div class="nav"><div id="nav">&nbsp;</div></div>
	<div id="content">
	<!--查询静态页面部分-->
	<div id="listview" style='display:none;'>
		<div class=form>
		<form id='white_enterform' name='white_enterform'>
			
			<input type=hidden id=readorderid name=readorderid>
			<div class="formTitle">客户信息</div>
				<table cellSpacing=0 cellPadding=4 width="100%" border=0>
						<tr>
					    <td width=30%><label for="userid">卡号</label> <span name='userid' id='userid'></span></td>
					    <td><label for="username">客户姓名</label> <span name="username" id="username"></span></td>	
					    	<td><label>账户剩余积分</label> <span name='points' id='points'></span></td>
					</tr>
					<tr>		
					     <td><label>账户可用积分</label> <span name='kpoints' id='kpoints'></span></td>
						<td><label>所用积分</label> <span name='vpoints' id='vpoints'></span></td>
						<td><button type="button" onclick="readCard()">读 卡</button></td>
					</tr>		
				</table>
			</div>
			<div id="listviewSave" align=center class="form">
			     <label>奖品名称</label><input type="text" name='giftName' id='giftName' onblur="checkV(this,'',true)" readonly style="background:#fffccc"/><a class=orga href=# onclick="selOrg()" return false;>&nbsp;&nbsp;&nbsp;</a><span class="des"> *</span>
				<label for=numeral>数量</label> <input id=numeral disabled onblur="checkV(this,'number',true)"><span class="des"> *</span>
				<td><button type="button" onclick="djfunc()" disabled id=localeEncash>增加</button> <button type="button" onclick="black()">取消</button></td>
			</div>
		</form>
		<div id="selectdetview" style='display:none;' align=center>
				<div class=listBorder>
					<div class="listBar">
						<div class="listTitle">礼品信息列表</div>
					</div>
					<div id=datalistLocale></div>		
				</div>	
			  <p> <button type="button" onclick="dayin()">领奖打印单据</button></p>
		        </div>
	</div>		
	<!--明细(新增)静态页面部分-->
	<div id="listviewmain">
		<div class=searchForm>
			<form id='listformmain' name='listformmain'>
			
			<input type=hidden id=giftLocation name=giftLocation>
				<table cellSpacing=0 cellPadding=4 width="100%" border=0>
					<tr>
					    <td width=50%><label for="orderCode">兑奖单编号</label> <input type="text" name='orderCode' id='orderCode'></td>
					      <td width=30%>客户姓名<input type="text" name='loginName' id='loginName'></td>
					     </tr>
					     <tr>
					     <td><label for=reqDateTime>申请日期&nbsp;&nbsp;</label> 
					     <input type="text" name="reqDateTime" onBlur="checkV(this,'_pastDate')"> <a href="#" onclick="showDatePicker($('listformmain').reqDateTime);" class="date" title="选择日期">&nbsp;&nbsp;</a></td>	
						<td>	<button type="button" onclick="showData1()">查 询</button></td>
					</tr>	
				</table>
			</form>
		</div>
			
		<div class=listBorder>
			<div class="listBar">
				<div class="listTitle">礼品兑奖单列表</div>
				<div class="listBtn" id="listBtn">
					<button type="button" onclick="onNewBookOrder()">创 建</button>
				</div>
			</div>
			<div id=datalist1></div>
		</div>	
	</div>

	<div id="showlistdiv" style='display:none;'>
		<form id='listform1' name='listform1'>
			<input type=hidden id=bookorderCode name=bookorderCode>
			<div class=form>
				<div class="formTitle">兑奖信息</div>
				<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable">
					 <tr>
					    <td><label >兑奖单编号</label> <span name='vorderid' id='vorderid'></span></td>
					 	  <td><label>状态</label> <span name="orderstate" id="orderstate"></span></td>
					</tr>
					<tr>		
					   <td><label>客户姓名</label> <span name="usernames" id="usernames"></span></td>
					   <td><label>申请日期</label> <span name='plantime' id='plantime'></span></td>
					</tr>	
					<tr>
						<td><label>操作员</label> <span name='oper' id='oper'></span></td>
					    <td><label>所用积分</label> <span name='point' id='point'></span></td>
					</tr>
				</table>
			</div>	 
		</form>
		<div class=listBard>
			<div class="listBar">
				<div class="listTitles">所兑礼品信息</div>
				
			</div>
			<div id=showlist>
			</div>		
			<div class="listBtn" id="listBtn">
			         <tr> 
			         <td>
			         <center>
			        <button  type="button" onclick="blacklist()">返回</button>  </center> </td></tr>
			
			</div>
		
		</div>	
	</div>
	<div class="printview" id="printview" style='display:none;'>
			<iframe id="requestframe" name="requestframe" style="width:100%;height:90%;overflow:auto;" frameborder=0 src=""></iframe>
		</div>
	

</body>
</HTML>